from django.db import models

# Create your models here.
# 文章
from auth_app.models import User


# 分类
class Type(models.Model):
    name = models.CharField(max_length=200, verbose_name='分类名称')
    description = models.CharField(max_length=200, verbose_name='分类描述', null=True)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)


# 文章
class Article(models.Model):
    user = models.ForeignKey(User, verbose_name="作者", on_delete=models.CASCADE)
    title = models.CharField(max_length=50, verbose_name='标题')
    cover = models.CharField(max_length=200, verbose_name='封面', null=True)
    summary = models.CharField(max_length=500, verbose_name='摘要', null=True)
    tag = models.CharField(max_length=1000, verbose_name='标签', null=True)
    type = models.ForeignKey(Type, verbose_name="文章类型", on_delete=models.SET_NULL, null=True)
    content = models.TextField(verbose_name='内容', null=True)
    status = models.IntegerField(verbose_name='状态：1=发布｜2=草稿', default=2)
    rang = models.IntegerField(verbose_name='范围：1=公开｜2=私密', default=1)
    examine = models.IntegerField(verbose_name='审核：0=待审核｜1=通过｜2=不通过', default=0)
    readers = models.IntegerField(verbose_name='阅读人数', default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)


# 收藏
class Collect(models.Model):
    article = models.ForeignKey(Article, verbose_name="文章id", on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name="用户id", on_delete=models.CASCADE)
    is_read = models.IntegerField(verbose_name='是否已读：0=否｜1=是', default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)


# 关注
class Follow(models.Model):
    follower = models.ForeignKey(User, verbose_name="关注人", on_delete=models.CASCADE, related_name="followers")
    being_followed = models.ForeignKey(User, verbose_name="被关注人", on_delete=models.CASCADE, related_name="being_followed")
    is_read = models.IntegerField(verbose_name='是否已读：0=否｜1=是', default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)


# 公告
class Notice(models.Model):
    title = models.CharField(max_length=50, verbose_name='标题')
    content = models.CharField(max_length=1000, verbose_name='内容')
    is_show = models.IntegerField(verbose_name='是否显示：0=否｜1=是', default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)


# 系统消息
class Message(models.Model):
    user = models.ForeignKey(User, verbose_name="用户id", on_delete=models.CASCADE)
    title = models.CharField(max_length=200, verbose_name='标题')
    content = models.CharField(max_length=1000, verbose_name='内容')
    is_read = models.IntegerField(verbose_name='是否已读：0=否｜1=是', default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)


# 评论
class Comment(models.Model):
    user = models.ForeignKey(User, verbose_name="评论用户", on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name="文章", on_delete=models.CASCADE)
    parent_comment = models.ForeignKey('self', verbose_name="父评论", on_delete=models.CASCADE, null=True)
    content = models.CharField(max_length=1000, verbose_name='内容')
    status = models.IntegerField(verbose_name='状态：1=发布｜2=待发布', default=2)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def toDict(self):
        data = {
            'id': self.id,
            'article_title':self.article.title,
            'article_user_name':self.article.user.first_name,
            'comment_user_name':self.user.first_name,
            'parent_comment_content':self.parent_comment.content if self.parent_comment else '',
            'parent_comment_user_name':self.parent_comment.user.first_name if self.parent_comment else '',
            'content': self.content,
            'create_time': self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            'update_time': self.update_time.strftime("%Y-%m-%d %H:%M:%S"),
            'status': self.status
        }
        return data

# 举报文章
class ReportArticle(models.Model):
    user = models.ForeignKey(User, verbose_name="用户id", on_delete=models.CASCADE)
    article = models.ForeignKey(Article, verbose_name="文章", on_delete=models.CASCADE)
    content = models.CharField(max_length=1000, verbose_name='内容')
    create_time = models.DateTimeField(auto_now_add=True)

    def toDict(self):
        data = {
            'id': self.id,
            'article_title':self.article.title,
            'reporter_name':self.user.first_name,
            'report_content':self.content,
            'create_time': self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
        }
        return data

# 举报评论
class ReportComment(models.Model):
    user = models.ForeignKey(User, verbose_name="用户id", on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment, verbose_name="评论", on_delete=models.CASCADE)
    content = models.CharField(max_length=1000, verbose_name='内容')
    create_time = models.DateTimeField(auto_now_add=True)

    def toDict(self):
        data = {
            'id': self.id,
            'comment_content':self.comment.content,
            'reporter_name':self.user.first_name,
            'report_content':self.content,
            'create_time': self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
        }
        return data
